/* $Id: RankingsPanel.java 102 2009-05-19 21:33:42Z henk $ */
package org.jugger.tournament.gui.view;

import java.util.Collection;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import org.jugger.tournament.model.TeamStats;
import org.jugger.tournament.model.Tournament;

import util.GUIUtils;
import util.I18N;

/**
 * @author Sven Hamer
 */
public class RankingsPanel extends JPanel {

	private static final long serialVersionUID = -1130621869140375816L;
	private Tournament tournament;

	public RankingsPanel(Tournament tournament) {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		reloadData(tournament);
	}

	private void addRankingTable(String title, Collection<TeamStats> ranking, Collection<TeamStats> rankingPreviousRound) {
		RankingTable rankingTable = new RankingTable(tournament, ranking, rankingPreviousRound, true);
		JPanel tablePanel = GUIUtils.createTitledPanelY(title);
		tablePanel.add(rankingTable.getTableHeader());
		tablePanel.add(rankingTable);
		add(tablePanel);
	}

	public void reloadData(Tournament tournament) {
		this.removeAll();
		this.tournament = tournament;

		int currentRoundNumber = tournament.countFinishedRounds()+1;

		addRankingTable(
				I18N.__("numberedRound", new Object[]{currentRoundNumber}) + " (" + I18N.__("currentRanking") + ")",
				tournament.getCurrentRanking(),
				currentRoundNumber > 1 ? tournament.getLastRanking() : null
		);
		for(int roundNumber=tournament.countFinishedRounds(); roundNumber>0; --roundNumber) {
			addRankingTable(
					I18N.__("numberedRound", new Object[]{roundNumber}),
					tournament.getRankingAfterRound(roundNumber),
					roundNumber > 1 ? tournament.getRankingAfterRound(roundNumber-1) : null
			);
		}
	}

}
